using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using jaam_butters.Agents;



namespace jaam_butters
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Jaam_Butters_Adventure : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        //AnimatedSprite sprite;

        KeyboardState keyboardstate;
        KeyboardState oldkeyboardstate;

        GameScreen activeScreen;
        StartScreen startScreen;
        GameMode gameMode;
        StoryScreen story;
        Texture2D[] storypic;

        Song bgm;
        bool songplayed = false;

        public Jaam_Butters_Adventure()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = 1280;
            graphics.PreferredBackBufferHeight = 720;
            Content.RootDirectory = "Content";
            this.IsMouseVisible = true;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            // Components.Add(new Jaam (this, new Vector2 (300,400)));
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Texture2D[] storypic = {Content.Load<Texture2D>("title"), Content.Load<Texture2D>("servantlabor"), Content.Load<Texture2D>("dark forest"), 
                                       Content.Load<Texture2D>("wizard"), Content.Load<Texture2D>("sleepy")};

            story = new StoryScreen(this, spriteBatch, storypic);
            Components.Add(story);
            story.Hide();

            startScreen = new StartScreen(this, spriteBatch, Content.Load<SpriteFont>("menufont"), Content.Load<Texture2D>("castle"));
            Components.Add(startScreen);
            startScreen.Hide();

            gameMode = new GameMode(this, spriteBatch, Content.Load<Texture2D>("background"));
            Components.Add(gameMode);
            gameMode.Hide();

            activeScreen = story;
            activeScreen.Show();

            bgm = Content.Load<Song>("hobbits");
            MediaPlayer.IsRepeating = true;
            //base.LoadContent();
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>

        private bool CheckKey(Keys theKey)
        {
            return keyboardstate.IsKeyUp(theKey) && oldkeyboardstate.IsKeyDown(theKey);
        }

        protected override void Update(GameTime gameTime)
        {
            keyboardstate = Keyboard.GetState();
            if (keyboardstate.IsKeyDown(Keys.Delete))
                this.Exit();

            if (activeScreen == story)
            {
                if (story.selectedItem +1 == story.storypic.Length)
                {
                    if (CheckKey(Keys.Enter))
                    {
                        activeScreen.Hide();
                        activeScreen = startScreen;
                        activeScreen.Show();
                    }
                }
            }

            else if (activeScreen == startScreen)
            {
                if (CheckKey(Keys.Enter))
                {
                    if (startScreen.SelectedItem == 0)
                    {
                        activeScreen.Hide();
                        activeScreen = gameMode;
                        activeScreen.Show();
                    }
                    if (startScreen.SelectedItem == 1)
                        this.Exit();
                }
            }
           base.Update(gameTime);
           oldkeyboardstate = keyboardstate;

           if (!songplayed)
           {
               MediaPlayer.Play(bgm);
               songplayed = true;
           }
        }
  

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
            base.Draw(gameTime);
            spriteBatch.End();
            // TODO: Add your drawing code here
        }
        public void ReturnToMenu()
        {
            activeScreen.Hide();
            activeScreen = startScreen;
            activeScreen.Show();
        }
    }
}
